//
//  StatusCell.swift
//  WeiBlog
//
//  Created by xiang on 15/10/20.
//  Copyright © 2015年 xiang. All rights reserved.
//

import UIKit
import SnapKit

let margin:CGFloat = 12
let IconWidth:CGFloat = 32
let ScreenWidth:CGFloat = UIScreen.mainScreen().bounds.width
let ScreenHeight:CGFloat = UIScreen.mainScreen().bounds.height

let RetweetedCellID = "RetweetedCellID"
let MyCellID = "MyCellID"

class StatusCell: UITableViewCell {

    //每一次给cell赋值时,都会调用两次(cell一次,行高一次)
    var viewModel:StatusViewModel?{
        didSet{
            
            //设置头部视图控件内容
            topView.viewModel = viewModel
            //设置正文
            contentLabel.text = viewModel?.status.text!
            
            //设置配图的内容(此时已经计算出了picturesView的size)
            picturesView.viewModel = viewModel
            
            //更新宽高约束
            picturesView.snp_updateConstraints{ (make) -> Void in
                
                make.height.equalTo(picturesView.bounds.height)
                make.width.equalTo(picturesView.bounds.width)
            }
        }
    }
    
    //计算cell的高度
    var rowHeight:CGFloat?

    override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
        super.init(style: .Default, reuseIdentifier: RetweetedCellID)
        
        //点击cell不会变色
        selectionStyle = .None
        self.setUI()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    // MARK:---懒加载
    ///头部view
    lazy var topView:TopViewOfCell = TopViewOfCell()
    ///正文view                                                                   
    lazy var contentLabel:UILabel = UILabel(text: "正文", fontSize: 13,screenSet: margin)
    ///尾部view
    lazy var bottomView:BottomViewOfCell = BottomViewOfCell()
    //配图view
    lazy var picturesView:PictureCollectionView = PictureCollectionView()
    
    

}


// MARK: 加载子控件
extension StatusCell
{
    func setUI(){
        
        //添加子控件
        contentView.addSubview(topView)
        contentView.addSubview(contentLabel)
        contentView.addSubview(picturesView)
        contentView.addSubview(bottomView)
        
//        topView.backgroundColor = UIColor.redColor()
//        contentLabel.backgroundColor = UIColor.redColor()
//        picturesView.backgroundColor = UIColor.redColor()
//        bottomView.backgroundColor = UIColor.redColor()
        
        //自动布局
        topView.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(contentView.snp_top)
            make.left.equalTo(contentView.snp_left)
            make.right.equalTo(contentView.snp_right)
            make.height.equalTo(2 * margin + IconWidth)
        }
        contentLabel.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(topView.snp_bottom).offset(margin)
            make.left.equalTo(contentView.snp_left).offset(margin)
        }
        bottomView.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(picturesView.snp_bottom).offset(margin)
            make.left.equalTo(contentView.snp_left)
            make.right.equalTo(contentView.snp_right)
            make.height.equalTo(30)
//            自己计算高度,就不需要这句代码,这句代码是配合系统自动计算高度使用的
//            make.bottom.equalTo(contentView.snp_bottom)
        }
        
    }
    
    
    
}

